//
//  SYXiuGaiDetailsView.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2019/4/25.
//  Copyright © 2019年 SanYi. All rights reserved.
//

#import "SYXiuGaiDetailsView.h"
#import "SYFenShuView.h"
#import "SYYueJuanHtmlTableViewCell.h"
#import "SYYueJuanTuPianTableViewCell.h"

@interface SYXiuGaiDetailsView () <UITableViewDataSource,UITableViewDelegate,SYFenShuViewDelegate>

@property (strong, nonatomic) UITableView *curTableView;
@property (strong, nonatomic) SYFenShuView *fenshuView;//分数
@property (strong, nonatomic) SYBackTopView *backTopView;
@property (strong, nonatomic) UIView *bgView;
@property (strong, nonatomic) SYYueJuanShiTiModel *yjstModel;//阅卷试题
@property (strong, nonatomic) NSMutableArray *dataListArr;
@property (strong, nonatomic) SYNoLabel *noLab;
@property (copy, nonatomic) NSString *correctId;//批改id

@end


@implementation SYXiuGaiDetailsView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initSubView];
        self.backgroundColor = [UIColor colorWithHexString:@"#EEEEEE"];
    }
    return self;
}
- (void)initSubView {
    
    [self addSubview:self.backTopView];
    [self.backTopView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(20);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(45);
    }];
    
    [self addSubview:self.bgView];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.backTopView.mas_bottom);
        make.left.equalTo(self).offset(14);
        make.bottom.right.equalTo(self).offset(-14);
    }];
    //分数
    [self.bgView addSubview:self.fenshuView];
    self.fenshuView.backgroundColor = [UIColor whiteColor];
    [self.fenshuView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.right.equalTo(self.bgView);
        make.height.mas_equalTo(140);
        make.left.equalTo(self.bgView.mas_left).offset(50);
        make.right.equalTo(self.bgView.mas_right).offset(-50);
    }];
    self.fenshuView.hidden = YES;
    
    //题目答案和作答
    [self.bgView addSubview:self.curTableView];
    [self.curTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView.mas_top);
        make.left.equalTo(self.bgView).offset(50);
        make.bottom.equalTo(self.bgView.mas_bottom).offset(-140);
        make.right.equalTo(self.bgView.mas_right).offset(-50);
    }];
    [self.curTableView registerCellWithNIbNames:@[@"SYYueJuanHtmlTableViewCell",@"SYYueJuanTuPianTableViewCell"]];
    _noLab = [[SYNoLabel alloc] initWithNameStr:@"" contView:self];
    [self.noLab mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.backTopView.mas_bottom);
        make.right.left.bottom.equalTo(self);
    }];
    
}

- (void)requestDataListAnswerIds:(NSString *)answerIds {
    
    self.correctId = answerIds;
    __weak typeof(self) weakSelf = self;
    [MBProgressHUD mj_showMessage:nil toView:self];
    [SYRequestAPI yj_requestCorrectServiceDetailsInfoAnswerIds:answerIds success:^(NSMutableArray *dataListArr) {
        [MBProgressHUD mj_hideHUDForView:self];
        weakSelf.dataListArr = dataListArr;
        weakSelf.yjstModel = weakSelf.dataListArr.firstObject;
        [weakSelf.curTableView reloadData];
        weakSelf.fenshuView.yjstModel = weakSelf.dataListArr.firstObject;
        weakSelf.fenshuView.correctId = weakSelf.correctId;
        weakSelf.backTopView.titleLab.text = weakSelf.yjstModel.studentName;
        if (dataListArr.count == 0) {
            self.noLab.hidden = NO;
        } else {
            self.fenshuView.hidden = NO;
            self.noLab.hidden = YES;
        }
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self];
        [MBProgressHUD zr_showError:error.message];
        self.noLab.hidden = NO;
    }];
    
}


- (void)requestDataListModel:(SYCorrectLogModel *)correctModel {
    self.correctId = correctModel.correctId;
    __weak typeof(self) weakSelf = self;
    [MBProgressHUD mj_showMessage:nil toView:self];
    [SYRequestAPI yj_requestCorrectServiceCorrectDetailsId:correctModel.correctId success:^(NSMutableArray *dataListArr) {
        [MBProgressHUD mj_hideHUDForView:self];
        weakSelf.dataListArr = dataListArr;
        weakSelf.yjstModel = weakSelf.dataListArr.firstObject;
        [weakSelf.curTableView reloadData];
        weakSelf.fenshuView.yjstModel = weakSelf.dataListArr.firstObject;
        weakSelf.fenshuView.correctId = weakSelf.correctId;
        weakSelf.backTopView.titleLab.text = weakSelf.yjstModel.studentName;
        if (dataListArr.count == 0) {
            self.noLab.hidden = NO;
        } else {
            self.fenshuView.hidden = NO;
            self.noLab.hidden = YES;
        }
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self];
        [MBProgressHUD zr_showError:error.message];
        self.noLab.hidden = NO;
    }];
    
    
}

#pragma mark - 表
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (self.dataListArr.count == 0) {
        return 0;
    } else {
        return 3;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        NSString *htmle = self.yjstModel.questionContent;
        
        NSAttributedString *attributed = [self attributedStringWithHTMLString:htmle];
        
        CGFloat height = [attributed  boundingRectWithSize:CGSizeMake(kScreen_WIDTH - 180 , CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.height;
        CGFloat ht = 40;
        if (height < 100) {
            return height + 50+ht;
        } else if (height < 200) {
            return height + 100+ht;
        } else {
            return height + 150+ht;
        }
    } else if (indexPath.section == 1) {
//        CGFloat templateHeight = [self.yjstModel.templateHeight integerValue];
//        CGFloat templateWidth = [self.yjstModel.templateWidth integerValue];
        CGFloat areaWidth = [self.yjstModel.areaHeight integerValue];

        CGFloat height = areaWidth *4;
        return height;
    } else {
        CGFloat areaWidth = [self.yjstModel.areaHeight integerValue];
        
        CGFloat height = areaWidth *4;
        return height;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        SYYueJuanHtmlTableViewCell *yjhtmlCell = [tableView dequeueReusableCellWithIdentifier:@"SYYueJuanHtmlTableViewCell" forIndexPath:indexPath];
        
        
        yjhtmlCell.htmlStr = self.yjstModel.questionContent;
        
        return yjhtmlCell;
    } else if (indexPath.section == 1) {
        SYYueJuanHtmlTableViewCell *yjhtmlCell = [tableView dequeueReusableCellWithIdentifier:@"SYYueJuanHtmlTableViewCell" forIndexPath:indexPath];
        
        yjhtmlCell.htmlStr = self.yjstModel.rightAnswerContent;
        
       // yjhtmlCell.yjstModel = self.yjstModel;
        
        return yjhtmlCell;
        
    } else {
        SYYueJuanTuPianTableViewCell *yjtpCell = [tableView dequeueReusableCellWithIdentifier:@"SYYueJuanTuPianTableViewCell" forIndexPath:indexPath];
        NSString *urlImage;
        if (self.yjstModel.correctPicture.length > 0) {
            urlImage = SYStringWithPickFormat(kSYWebViewUrlDefent, self.yjstModel.correctPicture);
        } else {
            urlImage = SYStringWithPickFormat(kSYWebViewUrlDefent, self.yjstModel.studentAnswer);
        }
        
        urlImage = [urlImage stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [yjtpCell.daanImageView sd_setImageWithURL:[NSURL URLWithString:urlImage]];
        return yjtpCell;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [UIView new];
    UILabel *headerLab = [UILabel titleColour:TITLECOLOR
                                   titleFount:14
                                     pingfang:@"PingFang-SC-Medium"
                                    alignment:NSTextAlignmentLeft
                                    titleLine:14];
    [headerView addSubview:headerLab];
    [headerLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(headerView.mas_left).offset(20);
        make.centerY.equalTo(headerView.mas_centerY);
    }];
    if (section == 0) {
        headerLab.text = @"题目";
    } else if (section == 1) {
        headerLab.text = @"答案";
    } else {
        headerLab.text = @"学生作答";
    }
    return headerView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *headerView = [UIView new];
    headerView.backgroundColor = [UIColor colorWithHexString:@"#CCCCCC"];
    return headerView;
}

//计算富文本高度
- (NSAttributedString *)attributedStringWithHTMLString:(NSString *)htmlString{
    NSDictionary *options = @{ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute :@(NSUTF8StringEncoding) };
    NSData *data = [htmlString dataUsingEncoding:NSUTF8StringEncoding];
    NSAttributedString *attributed = [[NSAttributedString alloc] initWithData:data options:options documentAttributes:nil error:nil];
    return attributed;
}
#pragma makr - SYFenShuViewDelegate
- (void)selectItemBtnIndex:(NSInteger)index fenshuViewType:(SYFenShuViewType)type {
    switch (type) {
        case SYFenShuViewTypeCheXiao:
        {
            self.yjstModel.correctPicture = @"";
            [self.curTableView reloadData];
        }
            break;
            
        default:
            break;
    }
    
    
}



- (UITableView *)curTableView {
    if (!_curTableView) {
        _curTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
        
        _curTableView.delegate = self;
        _curTableView.dataSource = self;
        _curTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _curTableView.backgroundColor = [UIColor whiteColor];
        //        [_curTableView registerCellWithClassNames:@[@"SYAttendLectureTableViewCell"]];
        // [_curTableView registerCellWithNIbNames:@[@"SYAttendLectureTableViewCell"]];
    }
    return _curTableView;
}
- (SYFenShuView *)fenshuView {
    if (!_fenshuView) {
        _fenshuView = [[NSBundle mainBundle] loadNibNamed:@"SYFenShuView" owner:nil options:nil].firstObject;
        //        _fenshuView.backgroundColor = [UIColor yellowColor];
        _fenshuView.xiayigeBtn.hidden = YES;
        _fenshuView.shangyigeBtn.hidden = YES;
        _fenshuView.delegate = self;
        _fenshuView.isDetials = YES;
    }
    return _fenshuView;
}

- (SYBackTopView *)backTopView {
    if (!_backTopView) {
        _backTopView = [[SYBackTopView alloc] initWithTitleName:@"" baseView:self];
    }
    return _backTopView;
}
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.backgroundColor = [UIColor whiteColor];
    }
    return _bgView;
}

@end
























